<template>
  <el-dialog v-model="visible" :title="title" width="520" class="public-dialog" show-footer show-zoom resize :close-on-click-modal="false"
    :close-on-press-escape="false" @close="close">
    <template #default>
      <el-form ref="formRef" :rules="rules" :model="form" label-align="right" label-width="120" size="default"
        label-position="top">
        <el-form-item label="所属系统" prop="home_system">
          <el-select v-model="form.home_system" placeholder="系统" class="w-full" clearable>
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="枚举名称">
          <el-input class="w-full" v-model="form.tag_desc" placeholder="枚举名称"></el-input>
        </el-form-item>
        <el-form-item label="枚举值">
          <el-input class="w-full" v-model="form.name" placeholder="枚举值"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch v-model="form.status" inline-prompt active-text="启用" inactive-text="禁用" :active-value="1"
            :inactive-value="2" />
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div class="dialog-button">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="save(formRef)">保 存</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup name="ComModal">
// import { getAreaDetail, saveArea, updateArea } from '../api/index'
// import { getWareData } from '@/api/public'
import { addExportTag, putExportTag, getExportTagDetail } from '../../api/index.js'
import { reactive, ref, toRefs } from 'vue'
import { ElMessage } from 'element-plus'

const visible = ref(false)
const loading = ref(false)
const formRef = ref(null)
const emits = defineEmits(['query'])

const props = defineProps({
  options: {
    type: Object,
    default: {},
  },
})

const { options } = toRefs(props)

const data = reactive({
  form: {},
  title: '新增',
  handleType: null,
  rules: {
    home_system: [{
      required: true, message: '请选择所属系统', trigger: 'change'
    }]
  },
})
const { form, title, handleType, rules, } = toRefs(data)

const statusMap = new Map([
  ['add', '新增导出枚举'],
  ['edit', '编辑导出枚举'],
  ['view', '查看'],
])

const open = (type, id) => {
  title.value = statusMap.get(type)
  handleType.value = type
  visible.value = true
  console.log(id)
  if (id) getDetail(id)
}

const initForm = () => {
  form.value = getForm()
}
// 初始化
const getForm = () => {
  return {
    status: 1,
    name: '',
    home_system: ''
  }
}

onMounted(() => {

  initForm()
})

const getDetail = async (id) => {
  try {
    const res = await getExportTagDetail(id)
    form.value = res.data
  } catch (e) {
    throw Error(e)
  }
}



const save = () => {
  if (!formRef.value) return
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      console.log(form.value.tag_id)
      if (form.value.tag_id) {
        putExportTag(form.value.tag_id, form.value)
          .then((res) => {
            ElMessage.success(res.message)
            emits('query')
            close()
          })
          .finally((e) => {
            loading.value = false
          })
      } else {
        addExportTag(form.value)
          .then((res) => {
            ElMessage.success(res.message)
            emits('query')
            close()
          })
          .finally((e) => {
            loading.value = false
          })
      }
    }
  })
}
const close = () => {
  initForm()
  formRef.value.resetFields();
  visible.value = false
}
defineExpose({ open })
</script>
<style lang="scss" scoped>
.borderNone {
  :deep(.el-input__wrapper) {
    border: none;
    box-shadow: none
  }
}
</style>
